import 'package:flutter/material.dart';

class HomeTab extends StatefulWidget{
  const HomeTab ({super.key});

  @override
  State<StatefulWidget> createState() =>_HomeTabState();
}

class _HomeTabState extends State<HomeTab> with AutomaticKeepAliveClientMixin{
  int _counter = 0;

  @override
  bool get wantKeepAlive => true;

  void _incrementCounter(){
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);//必须调用
    return Scaffold(
      appBar: AppBar(
        title: const Text('首页'),
        centerTitle: true,
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const Icon(Icons.home,size:100,color:Colors.blue),
            const SizedBox(height: 20),
            Text('计数器：$_counter',style: const TextStyle(fontSize: 24)),
            ElevatedButton(onPressed: _incrementCounter,
                child: const Text('增加计数'),),
            const SizedBox(height: 20),
            const Text('状态会被保持，切换tab不会重置',
            style: TextStyle(color: Colors.grey),)
          ],
        ),
      ),
    );
  }
}